SUBROUTINE BCAST_SCAVAR(irank_send,size_MPI,comm3d,rank_comm3d,scavar)

! This subroutine broadcasts single scalar variable scavar from 
! the process with isend = 1 to all other processes (isend = 0)

!use MPI
use MPI_VARIABLES, only : &
& bcast_scavar_indic
use TIMING_MOD, only : &
& proc_time

implicit none

include 'mpif.h'


! Input/output variables
integer(4), intent(in) :: irank_send
integer(4), intent(in) :: size_MPI, comm3d, rank_comm3d
real(8), intent(inout) :: scavar

! Local variables
integer(4), parameter :: maxitag = 1000

integer(4) :: irank, ierr_MPI
integer(4) :: status_MPI(MPI_STATUS_SIZE)
integer(4), save :: itag = 0
integer(4) :: isend_send(0:size_MPI-1), isend_recv(0:size_MPI-1)

real(8) :: work_real
real(8) :: pr_time, pro_time

!pr_time = MPI_WTIME(ierr_MPI)
call cpu_time(pr_time)

!itag = itag + 1

!if (isend == 1) then
!  do irank = 0, size_MPI-1
!    if (irank /= rank_comm3d) then
!      call MPI_SEND(scavar, 1, MPI_DOUBLE_PRECISION, irank, itag, &
!      & comm3d, ierr_MPI)
!    endif
!  enddo
!elseif (isend == 0) then
!  call MPI_RECV(scavar, 1, MPI_DOUBLE_PRECISION, MPI_ANY_SOURCE, &
!  & itag, comm3d, status_MPI, ierr_MPI)
!else
!  write(*,*) 'Wrong input parameter isend in BCAST_SCAVAR: STOP'
!  STOP
!endif

!if (itag == maxitag) itag = 0

!isend_send(:) = isend
!call MPI_ALLTOALL(isend_send, 1, MPI_INTEGER, &
!&                 isend_recv, 1, MPI_INTEGER, &
!&                 comm3d, ierr_MPI)
!do irank = 0, size_MPI-1
!  if (isend_recv(irank) == 1) irank_send = irank
!enddo

call MPI_BCAST(scavar, 1, MPI_DOUBLE_PRECISION, irank_send, &
&              comm3d, ierr_MPI)

!proc_time(bcast_scavar_indic,2) = proc_time(bcast_scavar_indic,2) + &
!& MPI_WTIME() - pr_time
call cpu_time(pro_time)
proc_time(bcast_scavar_indic,2) = proc_time(bcast_scavar_indic,2) + &
& pro_time - pr_time

!call MPI_BARRIER(comm3d,ierr_MPI)

END SUBROUTINE BCAST_SCAVAR

